package com.ohc.leetcode.栈.repeat;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Stack;

/**
 * 2022年5月13日20:44:58
 */
public class 有效的括号04 {
    public static boolean isValid(String s) {
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        Map<Character, Character> map = new HashMap<>();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                stack.push(c);
            } else if (c == '[') {
                stack.push(c);
            } else if (c == '{') {
                stack.push(c);
            } else {
                if (stack.isEmpty())
                    return false;
                Character pop = stack.pop();
                if (map.get(c) != pop) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        boolean valid = isValid("([)]");
        System.out.println("valid = " + valid);

    }
}
